﻿using System;
using System.Collections;
using System.Linq;

namespace Framework.IISUtility
{
    /// <summary>
    /// IISWebVirtualDirCollection
    /// </summary>
    public class IISWebVirtualDirCollection : CollectionBase
    {
        /// <summary>
        ///
        /// </summary>
        public IISWebServer Parent = null;
        /// <summary>
        ///
        /// </summary>
        public IISWebVirtualDir this[int Index]
        {
            get
            {
                return (IISWebVirtualDir)List[Index];

            }
        }
        /// <summary>
        ///
        /// </summary>
        public IISWebVirtualDir this[string Name]
        {
            get
            {
                Name = Name.ToLower();
                return List.Cast<IISWebVirtualDir>().FirstOrDefault(list => list.Name.ToLower() == Name);
            }
        }
        internal void Add_(IISWebVirtualDir WebVirtualDir)
        {
            try
            {
                List.Add(WebVirtualDir);
            }
            catch
            {
                throw (new Exception("发生意外错误，可能是某节点将该节点的上级节点作为它自己的子级插入"));
            }

        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="WebVirtualDir"></param>
        public void Add(IISWebVirtualDir WebVirtualDir)
        {
            WebVirtualDir.Parent = Parent;
            try
            {
                List.Add(WebVirtualDir);

            }
            catch
            {
                throw (new Exception("发生意外错误，可能是某节点将该节点的上级节点作为它自己的子级插入"));
            }
            IISManagement.CreateIISWebVirtualDir(WebVirtualDir, true);

        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="WebVirtualDirs"></param>
        public void AddRange(IISWebVirtualDir[] WebVirtualDirs)
        {
            for (int i = 0; i <= WebVirtualDirs.GetUpperBound(0); i++)
            {
                Add(WebVirtualDirs[i]);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="WebVirtualDir"></param>
        public void Remove(IISWebVirtualDir WebVirtualDir)
        {
            for (int i = 0; i < List.Count; i++)
            {
                if (List[i] == WebVirtualDir)
                {
                    List.RemoveAt(i);
                    IISManagement.RemoveIISWebVirtualDir(WebVirtualDir);
                    return;
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="Parent"></param>
        public IISWebVirtualDirCollection(IISWebServer Parent)
        {
            this.Parent = Parent;
        }
    }
}
